package com.wish.group05.house.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.group05.base.entity.*;
import com.wish.group05.base.repository.RoomInfoRepository;
import com.wish.group05.base.service.*;
import com.wish.group05.common.core.text.UUID;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.house.dto.HouseAddDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 作者:李皞
 * 日期:2022/5/31
 **/
@RestController
@RequestMapping("house")
public class TbHouseInfoController {
    @Autowired
    private IHouseInfoService houseInfoService;

    @Autowired
    private IRoomInfoService roomInfoService;

    @Autowired
    private IRelationHousePictureService relationHousePictureService;

    @Autowired
    private IRelationRoomPictureService relationRoomPictureService;

    @Autowired
    private IRoomReservationService roomReservationService;

    @Autowired
    private IPremisesInfoService premisesInfoService;

    @Autowired
    private IHouseStyleService houseStyleService;

    @Autowired
    private IRelationStylePictureService relationStylePictureService;

    @Autowired
    private RoomInfoRepository roomInfoRepository;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 全量同步
     */
    @GetMapping("saveEs")
    public void saveEs(){
        List<RoomInfo> list = roomInfoService.getList(null);
        roomInfoRepository.saveAll(list);

//        NativeSearchQuery build = new NativeSearchQueryBuilder().build();
//        SearchHits<HouseInfo> search = elasticsearchRestTemplate.search(build, HouseInfo.class);
//        System.err.println(search);
    }

    /**
     * 添加集中式房源
     *
     * @return
     */
    @PostMapping("saveFocus")
    public AjaxResult saveFocus(@RequestBody HouseAddDto houseAddDto) {
        System.err.println(houseAddDto);
        if (houseAddDto.getPublicIds() == null || houseAddDto.getPublicIds().size() <= 0) {
            return AjaxResult.error("请选择公共配套");
        }
        if (houseAddDto.getPrivateIds() == null || houseAddDto.getPrivateIds().size() <= 0) {
            return AjaxResult.error("请选择房间配套");
        }
        //拼接信息
        substring(houseAddDto);



        //添加房源主键
        houseAddDto.setId(UUID.randomUUID().toString());
        //添加主表house
        saveHouse(houseAddDto);

        //添加集中式表
        saveHouseStyle(houseAddDto);

        //添加房间表room
        saveRoom(houseAddDto);

        //添加公共配套图片表
        savePublicPic(houseAddDto);

        return AjaxResult.success();
    }

    /**
     * 添加分散式房源
     *
     * @param houseAddDto
     * @return
     */
    @PostMapping("saveDisperse")
    public AjaxResult saveDisperse(@RequestBody HouseAddDto houseAddDto) {

        if (houseAddDto.getPublicIds() == null || houseAddDto.getPublicIds().size() <= 0) {
            return AjaxResult.error("请选择公共配套");
        }
        if (houseAddDto.getPrivateIds() == null || houseAddDto.getPrivateIds().size() <= 0) {
            return AjaxResult.error("请选择房间配套");
        }
        //拼接信息
        substring(houseAddDto);

        //添加房源主键
        houseAddDto.setId(UUID.randomUUID().toString());

        //添加主表house
        saveHouse(houseAddDto);

        //添加房间表room
        saveRoom(houseAddDto);

        //添加公共配套图片表
        savePublicPic(houseAddDto);

        return AjaxResult.success();
    }

    /**
     * 拼接
     * @param houseAddDto
     * @return
     */
    public HouseAddDto substring(HouseAddDto houseAddDto){
        //拼接公共配套
        String publicIds = "";
        for (Integer publicId : houseAddDto.getPublicIds()) {
            publicIds += "," + publicId;
        }
        publicIds = publicIds.substring(1);
        houseAddDto.setPublicFacility(publicIds);
        //拼接房间配套
        String privateIds = "";
        for (Integer privateId : houseAddDto.getPrivateIds()) {
            privateIds += "," + privateId;
        }
        privateIds = privateIds.substring(1);
        houseAddDto.setPrivateFacility(privateIds);
        return houseAddDto;
    }

    /**
     * 添加集中式表
     *
     * @param houseAddDto
     */
    public void saveHouseStyle(HouseAddDto houseAddDto) {
        //copy属性
        HouseStyle houseStyle = new HouseStyle();
        BeanUtils.copyProperties(houseAddDto,houseStyle);
        houseStyle.setId(UUID.randomUUID().toString());
        houseStyle.setHouseId(houseAddDto.getId());
        houseStyleService.save(houseStyle);

        //添加房源类型图片表
        RelationStylePicture relationStylePicture = new RelationStylePicture();
        relationStylePicture.setStyleId(houseStyle.getId());
        relationStylePicture.setPictureUrl(houseAddDto.getRoomPicture());
        relationStylePictureService.save(relationStylePicture);
    }


   /**
     * 添加公共配套图片表
     *
     * @param houseAddDto
     */
    public void savePublicPic(HouseAddDto houseAddDto) {
        RelationHousePicture relationHousePicture = new RelationHousePicture();
        relationHousePicture.setPictureUrl(houseAddDto.getHousePicture());
        relationHousePicture.setHouseId(houseAddDto.getId());
        relationHousePictureService.save(relationHousePicture);
    }

    /**
     * 添加house表
     *
     * @param houseAddDto
     * @return
     */
    private void saveHouse(HouseAddDto houseAddDto) {
        HouseInfo houseInfo = new HouseInfo();
        BeanUtils.copyProperties(houseAddDto, houseInfo);

        //判断是否有该楼盘名称
        PremisesInfo premisesInfo = premisesInfoService.getOne(new QueryWrapper<PremisesInfo>().eq("name", houseAddDto.getPremisesName()));
        //为空的话添加一条楼盘信息
        if (premisesInfo == null) {
            PremisesInfo premises = new PremisesInfo();
            premises.setName(houseAddDto.getPremisesName());
            premisesInfoService.save(premises);
            //赋值楼盘id
            houseInfo.setPremisesId(premises.getId());
        } else {
            //赋值楼盘id
            houseInfo.setPremisesId(premisesInfo.getId());
        }

        houseInfo.setPublishStatus(1);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        houseInfo.setCreateTime(sdf.format(date));
        houseInfo.setDelFlg(true);

        houseInfoService.save(houseInfo);
    }

    /**
     * 添加房间表
     *
     * @param houseAddDto
     */
    private void saveRoom(HouseAddDto houseAddDto) {
        RoomInfo roomInfo = new RoomInfo();
        BeanUtils.copyProperties(houseAddDto, roomInfo);
        roomInfo.setId(UUID.randomUUID().toString());
        roomInfo.setHouseId(houseAddDto.getId());
        roomInfo.setFrontCover(houseAddDto.getRoomPicture());
        roomInfo.setBuildingName(houseAddDto.getBuildingNo());
        roomInfo.setStatus(0);
        roomInfo.setCreateTime(new Date());
        roomInfo.setDelFlg(true);

        //获取用户名
        SysUser sysUser = sysUserService.getById(houseAddDto.getManager());
        roomInfo.setUserName(sysUser.getUsername());
        //获取楼盘名称
        roomInfo.setPremisesName(houseAddDto.getPremisesName());
        //获取发布状态
        roomInfo.setPublishStatus(1);
        //房源增量同步es
        roomInfoRepository.save(roomInfo);

        roomInfoService.save(roomInfo);

        //添加房间配套图片表
        RelationRoomPicture relationRoomPicture = new RelationRoomPicture();
        relationRoomPicture.setPictureUrl(houseAddDto.getRoomPicture());
        relationRoomPicture.setRoomId(roomInfo.getId());
        relationRoomPictureService.save(relationRoomPicture);

        //添加房间关系表
        RoomReservation roomReservation = new RoomReservation();
        roomReservation.setCreateTime(new Date());
        roomReservation.setId(UUID.randomUUID().toString());
        roomReservation.setStatus(houseAddDto.getRoomStatus());
        roomReservation.setRoomId(roomInfo.getId());
        roomReservation.setUserId(houseAddDto.getManager());
        roomReservationService.save(roomReservation);
    }
}
